/*
 * BorrowReturnBooks.fx
 * Author: Yu Min-ting
 * Created on 2009-7-19, 12:19:22
 */

package com.sjtu.fx.user;

import com.sjtu.fx.tools.*;
import com.sjtu.fx.tools.SwingTable.*;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONArray;
import org.json.JSONObject;

import javafx.stage.Alert;

import java.lang.System;

public class In{
    public var bookName:String;
    public var checkInDate:String;
    public var state:String;
    public var checkOutDate:String;
}

public class Out{
    public var bookName:String;
    public var checkInDate:String;
    public var returnDate:String;
}

public class BorrowReturnBooks extends CustomNode {

    var panel:Panel;
    var checkIn:In[] = bind checkIn with inverse;
    var checkOut:Out[] = bind checkOut with inverse;
    public var selectionOut:Integer;
    public var selectionIn: Integer;
    var booklibraryinfoList:Integer[];

    var inTable:SwingTable = SwingTable{
        visible:false
        columns: [
                    TableColumn {
                        text: "Book Name"
                    },
                    TableColumn {
                        text: "Check in Date"
                    },
                    TableColumn {
                        text: "Check out Date"
                    },
                    TableColumn {
                        text: "State"
                    },
                ]
        rows: bind for (u in checkIn)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.bookName
                            },
                            TableCell {
                                text:bind u.checkInDate
                            },
                            TableCell {
                                text: bind u.checkOutDate
                            },
                            TableCell {
                                text: bind u.state
                            },
                        ]
                    }
        selection: bind selectionIn with inverse

    }

     var outTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Book Name"
                    },
                    TableColumn {
                        text: "Check in Date"
                    },
                    TableColumn {
                        text: "Promised Return Date"
                    }
                ]
        rows: bind for (u in checkOut)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.bookName
                            },
                            TableCell {
                                text: bind u.checkInDate
                            },
                            TableCell {
                                text: bind u.returnDate
                            }
                        ]
                    }
        selection: bind selectionOut with inverse

    }


    var returnBook = Button {
            text: "Return this Book"
            action: function() {
                if(selectionOut>=0){
                    userReturnBook(booklibraryinfoList[selectionOut]);
                    delete checkOut[selectionOut];
                }
            }
        }
     var renew = Button {
             text: "Renew this Book"
             action: function() {
                 if(selectionOut>=0){
                    // to do

                    delete checkOut[selectionOut];
                }
             }
         }

 var inManage: Rectangle = Rectangle{
        width:100 height:22
        x:15 y:5
        fill:Color.rgb(200,225,230)
            onMouseEntered: function( e: MouseEvent ):Void {
                inManage.fill = Color.rgb(152, 190, 198)
        }
            onMouseExited: function(e:MouseEvent) {
                inManage.fill = Color.rgb(200,225,230)
        }
            onMouseClicked: function( e: MouseEvent ):Void {
                inTable.visible = true;
                outTable.visible = false;
                returnBook.visible = false;
                renew.visible = false;
                getBorrowedList((this.parent.lookup("loginPanel") as Login).userid);

        }
    }
    var inText = Text {
        font: Font {size:14 name: "Verdana Bold"}
        fill: Color.rgb(11,51,60)
        content: "   Check In  "
        x:15 y:20
    };
    var outManage: Rectangle = Rectangle{
        width:100 height:20
        x:119 y:5
        fill:Color.rgb(200,225,230)
            onMouseEntered: function( e: MouseEvent ):Void {
                outManage.fill = Color.rgb(152, 190, 198)
        }
            onMouseExited: function(e:MouseEvent) {
                outManage.fill = Color.rgb(200,225,230)
        }
            onMouseClicked: function( e: MouseEvent ):Void {
                inTable.visible = false;
                outTable.visible = true;
                returnBook.visible = true;
                renew.visible = true;
                getReturnList((this.parent.lookup("loginPanel") as Login).userid);
        }
    }
     var outText = Text {
        font: Font {size:14 name: "Verdana Bold"}
        fill: Color.rgb(11,51,60)
        content: "Check Out "
        x:125 y:20

    };
     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }

    override protected function create() : Node {


        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "   Check In   |  Check Out  Management"
                    translateX: 0
                    translateY: 0
                },
                inManage, inText,
                outManage,outText,
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                        inTable
                    ]

                },
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                        outTable,
                        HBox{
                            spacing:50
                            content:[returnBook,renew]
                        }
                    ]

                },alert


            ]
        }
     }

     function getBorrowedList(userId:Integer){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/UserBorrowBookList"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
       //         System.out.println(result);
                json = new JSONObject(result);
                var bookNameList:JSONArray = json.getJSONArray("userBorrowBookList");
                var i:Integer = 0;
                while(i<bookNameList.length()){
                    var bookBookInfo:String[] = bookNameList.get(i).toString().split("#");
                    checkIn[i] = In{
                        bookName:bookBookInfo[0]
                        checkInDate:bookBookInfo[2]
                        state:bookBookInfo[4]
                        checkOutDate:bookBookInfo[3]
                        };
                    i++;
                }
            } finally {
                is.close();
            }
        }
    }.start();
     }

     function getReturnList(userId:Integer){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/UserReturnList"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
       //         System.out.println(result);
                json = new JSONObject(result);
                var bookNameList:JSONArray = json.getJSONArray("userReturnBookList");
                var i:Integer = 0;
                while(i<bookNameList.length()){
                    var bookBookInfo:String[] = bookNameList.get(i).toString().split("#");
                    checkOut[i] = Out{
                        bookName:bookBookInfo[0]
                        checkInDate:bookBookInfo[2]
                        returnDate:bookBookInfo[3];
                        };
                    i++;
                    booklibraryinfoList[i] = Integer.parseInt(bookBookInfo[4]);
                }
            } finally {
                is.close();
            }
        }
    }.start();
     }

     function userReturnBook(booklibraryinfo:Integer){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/ReturnBook"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userBookInfoId",booklibraryinfo);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
       //         System.out.println(result);
                alert.content = result;
                alert.visible = true;
                //Alert.inform(result);
            } finally {
                is.close();
            }
        }
    }.start();

     }


}
